package com.example.demo.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.example.demo.anno.Loggin;
import com.example.demo.error.Result;
import com.example.demo.error.ResultCode;
import com.example.demo.error.ResultGenerator;
import com.example.demo.pojo.RegisterRequest;
import com.example.demo.pojo.User;
import com.example.demo.service.UserService;
import org.apache.tomcat.util.buf.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@SuppressWarnings("AliDeprecation")
@RestController
@RequestMapping("/users")
public class UserController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Autowired
    UserService userService;

    /**
     * 查询所有
     * @param request
     * @return
     */
    @PostMapping("/all")
    @Loggin
    public Result<?> select(@RequestBody RegisterRequest request) {
        if (StrUtil.isBlank(request.getId())) {
            //查询全部
            List<User> ids = userService.getAllUser(request);
            return ResultGenerator.genSuccessResult(ids);
        }
        List<User> userById = userService.getUserById(request.getId());
        if (!userById.isEmpty()) {
            return ResultGenerator.genSuccessResult(userById);
        } else {
            // 处理找不到用户的情况
            return ResultGenerator.genSpecialResult(ResultCode.FAIL,"没找到");
        }
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<?> selectById(@PathVariable String id) {
        List<User> userById = userService.getUserById(id);
        if (userById != null && !userById.isEmpty()) {
            return ResultGenerator.genSuccessResult(userById);
        } else {
            return ResultGenerator.genSpecialResult(ResultCode.DEFEAT, "未找到用户");
        }
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @PostMapping("/update")
    public Result<?> update(@RequestBody User user) {
        String id = user.getId().toString();
        List<User> userById = userService.getUserById(id);
        if (userById == null || userById.isEmpty()) {
            return ResultGenerator.genFailResult("未查询到需要修改的用户");
        }
        int num = userService.updateById(user);
        return ResultGenerator.genSuccessResult(num);
    }

    /**
     * 新增用户
     */
    @PostMapping("/insert")
    public Result<?> insert(@RequestBody User user) {
        int num = userService.insertUser(user);
        return ResultGenerator.genSuccessResult(num);
    }

    /**
     * 删除用户
     * @param user
     * @return
     */
    @PostMapping("/delete")
    public Result<?> delete(@RequestBody User user) {
        int num = userService.deleteUser(user);
        return ResultGenerator.genSuccessfulResult(num,"删除成功");
    }
}
